package bb.lanxing.databinding.sprint.routebook;

import java.text.MessageFormat;

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.core.internal.view.SupportMenu;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.model.database.User;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import bb.lanxing.util.text.TextUtils;
import gov.nist.core.Separators;

public class RouteInfoTitleModel extends BaseObservable {
    private RouteBook routeBook;
    private final Context mContext;

    public RouteInfoTitleModel(RouteBook routeBook, Context mContext) {
        this.routeBook = routeBook;
        this.mContext = mContext;
    }

    public void updateData(RouteBook routeBook) {
        this.routeBook = routeBook;
        notifyChange();
    }

    @Bindable
    public Drawable getSportStyleImage() {
        int sport = this.routeBook.getSport();
        Drawable drawable;
        switch (sport) {
        case Workout.TYPE_OTHER -> {
            drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_other);
        }
        case Workout.TYPE_WALKING -> {
            drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_walking);
        }
        case Workout.TYPE_RUNNING -> {
            drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_running);
        }
        case Workout.TYPE_SWIMMING -> {
            drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_swimming);
        }
        case Workout.TYPE_SKIING -> {
            drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_skiing);
        }
        case Workout.TYPE_CYCLING -> {
            drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_cycling);
        }
        default -> drawable = this.mContext.getResources().getDrawable(R.drawable.ic_history_other);
        }
        return drawable;
    }

    @Bindable
    public String getRouteTitle() {
        return this.routeBook.getTitle() == null ? "" : this.routeBook.getTitle();
    }

    @Bindable
    public boolean getRouteNumVisiable() {
        return this.routeBook.getServerId() > 0;
    }

    @Bindable
    public String getRouteNum() {
        return Separators.POUND + this.routeBook.getServerId();
    }

    @Bindable
    public String getLushuDistance() {
        return this.mContext.getString(R.string.str_fm_unit_km_with_space,
                CommonUtil.getFormatDistance(this.routeBook.getDistance()));
    }

    @Bindable
    public String getElevationGain() {
        return this.mContext.getString(R.string.str_fm_unit_m_with_space,
                String.valueOf(Math.abs((int) this.routeBook.getElevationGain())));
    }

    @Bindable
    public String getSlopValue() {
        return MessageFormat.format("{0} %", this.routeBook.getAvgGrade());
    }

    @Bindable
    public String getAltitudeValue() {
        return this.mContext.getString(R.string.str_fm_unit_m_with_space,
                String.valueOf(Math.round(this.routeBook.getMaxAltitude() - this.routeBook.getMinAltitude())));
    }

    @Bindable
    public String getMapViewImageUrl() {
        return routeBook.getImageUrl();
    }

    @Bindable
    public String getMapAltitudeViewUrl() {
        return this.routeBook.getSlopeThumbnail();
    }

    @Bindable
    public boolean getThreedLushuVisiable() {
        return !TextUtils.isEmptyOrNull(this.routeBook.getThreedLushu());
    }

    @Bindable
    public String getRouteCreateUserName() {
        if (this.routeBook.getUserId() <= 0) {
            return "";
        }
        if (this.routeBook.getUserId() == SharedManager.getInstance().getUserId()) {
            User signinUser = App.getContext().getSigninUser();
            if (signinUser == null) {
                return this.routeBook.getUsername();
            }
            return signinUser.getName();
        }
        return this.routeBook.getUsername();
    }

    @Bindable
    public int getRouteCreateUserNameColor() {
        return this.routeBook.getUserId() == ((long) SharedManager.getInstance().getUserId()) ? SupportMenu.CATEGORY_MASK
                : this.mContext.getResources().getColor(R.color.detail_text_username);
    }

    @Bindable
    public boolean getRouteCreateUserClickEnable() {
        return this.routeBook.getUserId() != ((long) SharedManager.getInstance().getUserId());
    }

    @Bindable
    public String getRouteDesc() {
        if (TextUtils.isEmptyOrNull(this.routeBook.getDescription())) {
            return this.mContext.getString(R.string.lushu_info_no_desc);
        }
        return this.routeBook.getDescription();
    }

    @Bindable
    public boolean getSegmentUserContainerVisiable() {
        return this.routeBook.getTrackSegment() != null;
    }

    @Bindable
    public String getSegmentUserName() {
        return this.routeBook.getTrackSegment() != null ? this.routeBook.getTrackSegment().getUserName() : "";
    }

    @Bindable
    public String getSegmentUserTime() {
        return this.routeBook.getTrackSegment() != null ? DateUtil.format(this.routeBook.getTrackSegment().getDuration(), 2)
                : "";
    }

    @Bindable
    public String getCommentCount() {
        return this.mContext.getString(R.string.lushu_info_label_comment_num,
                String.valueOf(this.routeBook.getCommentCount()));
    }

    @Bindable
    public boolean getRouteCommentContainerVisiable() {
        return this.routeBook.getCommentCount() != 0;
    }

    @Bindable
    public boolean getCommentEmptyVisiable() {
        return this.routeBook.getCommentCount() == 0;
    }
}
